package com.wgts.wgts.mvp.view.activity.todayspecial;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.SpecialProductEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomePageCurrencyAdapter extends BaseLoadMoreAdapter<RecyclerView.ViewHolder> {


    private static final int TITLE = 0x00001;
    private static final int HOLDER = 0x00002;


    private Context context;

    private List<SpecialProductEntity.Entity> srcData;
    private BuyOnclick buyOnclick;
    private PutAwayOnclick putAwayOnclick;

    public HomePageCurrencyAdapter(Context context, List<SpecialProductEntity.Entity> srcData) {
        super(context);
        this.context = context;
        this.srcData = srcData;
    }

    public OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }


    @Override
    public void onBindBodyViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof TodaySpecialTitleViewHolder) {
            MoHuGlide.getInstance().glideImgsFormList(((TodaySpecialTitleViewHolder) viewHolder).imgTitle, "", context);
        }
        if (viewHolder instanceof TodaySpecialViewHolder) {
            if (AppInfor.getInstance().getUserState() < 2){
                ((TodaySpecialViewHolder) viewHolder).linePutAway.setVisibility(View.GONE);
                ((TodaySpecialViewHolder) viewHolder).placeholderText.setVisibility(View.GONE);
                MoHuGlide.getInstance().glideImg(context, srcData.get(i).productImage, ((TodaySpecialViewHolder) viewHolder).imgProduct);
                ((TodaySpecialViewHolder) viewHolder).tvProduce.setText(srcData.get(i).productName);
                ((TodaySpecialViewHolder) viewHolder).tvPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));
                ((TodaySpecialViewHolder) viewHolder).tvPrice.setText("￥" + srcData.get(i).retailPrice);
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP,11);
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.content_text));
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setText("￥" + srcData.get(i).marketPrice);
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                ((TodaySpecialViewHolder) viewHolder).tvProduceSolve.setText("已售" + srcData.get(i).sellCount);
            }else {
                ((TodaySpecialViewHolder) viewHolder).linePutAway.setVisibility(View.GONE);
                ((TodaySpecialViewHolder) viewHolder).placeholderText.setVisibility(View.VISIBLE);
                ((TodaySpecialViewHolder) viewHolder).tvProduce.setText(srcData.get(i).productName);
                ((TodaySpecialViewHolder) viewHolder).tvPrice.setTextColor(ContextCompat.getColor(context, R.color.title_text));
                ((TodaySpecialViewHolder) viewHolder).tvPrice.setText("￥" + srcData.get(i).retailPrice);
                MoHuGlide.getInstance().glideImg(context, srcData.get(i).productImage, ((TodaySpecialViewHolder) viewHolder).imgProduct);
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));
                ((TodaySpecialViewHolder) viewHolder).tvOldPrice.setText(srcData.get(i).marketPrice);
                ((TodaySpecialViewHolder) viewHolder).tvProduceSolve.setText("已售" + srcData.get(i).sellCount);
            }


            ((TodaySpecialViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null) {
                        onClickListener.onClick(v, i);
                    }
                }
            });

//            ((TodaySpecialViewHolder) viewHolder).linearBuy.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(buyOnclick != null){
//                        buyOnclick.buyOnclick(v, i);
//                    }
//                }
//            });
//
//            ((TodaySpecialViewHolder) viewHolder).linePutAway.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (putAwayOnclick != null){
//                        putAwayOnclick.putAwayOnclick(v, i);
//                    }
//                }
//            });


        }
    }

    @Override
    public int getBodyItemCount() {
        return srcData.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return HOLDER;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TITLE) {
            return new TodaySpecialTitleViewHolder(LayoutInflater.from(context).inflate(R.layout.today_special_title_view_holder, viewGroup, false));
        } else {
            return new TodaySpecialViewHolder(LayoutInflater.from(context).inflate(R.layout.home_page_currency_item, viewGroup, false));
        }
    }

    public class TodaySpecialViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout linearLayout;
        @BindView(R.id.img_product)
        ImageView imgProduct;
        @BindView(R.id.tv_produce)
        TextView tvProduce;
        @BindView(R.id.tv_produce_solve)
        TextView tvProduceSolve;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.placeholder_text)
        TextView placeholderText;
        @BindView(R.id.tv_old_price)
        TextView tvOldPrice;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.linear_buy)
        LinearLayout linearBuy;
        @BindView(R.id.line_put_away)
        LinearLayout linePutAway;

        public View itemView;


        public TodaySpecialViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.parent_ly);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public class TodaySpecialTitleViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgTitle;

        public TextView tvTitle;

        public TodaySpecialTitleViewHolder(@NonNull View itemView) {
            super(itemView);
            imgTitle = itemView.findViewById(R.id.img_title);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }
    }

    private boolean isTitle(int postion) {
        if (postion == 0) {
            return true;
        }
        return false;
    }

    private boolean isHolder(int postion) {
        if (postion > 1) {
            return true;
        }
        return false;
    }

    public interface OnClickListener {
        void onClick(View view, int i);
    }

    public interface BuyOnclick{
        void buyOnclick(View view, int position);
    }

    public interface PutAwayOnclick{
        void putAwayOnclick(View view, int position);
    }

    public void setBuyOnclick(BuyOnclick buyOnclick){
        this.buyOnclick = buyOnclick;
    }

    public void setPutAwayOnclick(PutAwayOnclick putAwayOnclick){
        this.putAwayOnclick = putAwayOnclick;
    }
}
